<?php

class Common {

	/**
	 * $param 变量
	 * @return void
	 */
	public static function debug() {
		echo '<pre>';
		foreach (func_get_args() as $v) {
			print_r($v);
			echo "\n";
		}
		echo '</pre>';
		exit;
	}

	private static function _getScriptName() {
		$f = basename($_SERVER['SCRIPT_FILENAME']);
		return substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], $f) + strlen($f));
	}

	/**
	 * 获取当前 Path 的 URI
	 * @param $path
	 * @return string
	 */
	public static function getURI($path) {
		return self::_getScriptName() . $path;
	}

	/**
	 * 获取当前 API Path 的 URI
	 * @param $path
	 * @return string
	 */
	public static function getAPIURI($path) {
		return self::_getScriptName() . '/plugin' . $path;
	}

	/**
	 * 获取当前 API Path 的静态 URI
	 * @param $path
	 * @return string
	 */
	public static function getAPIStaticURI($path) {
		$scriptName = self::_getScriptName();
		if ($scriptName == '/index.php') {
			return $path;
		}
		return dirname($scriptName) . $path;
	}

	/**
	 * 获取随机字符、数字
	 * @param $length
	 * @param $numeric
	 * @return string
	 */
	public static function random($length, $numeric = 0) {
		$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
		$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
		if ($numeric) {
			$hash = '';
		} else {
			$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
			$length--;
		}
		$max = strlen($seed) - 1;
		for ($i = 0; $i < $length; $i++) {
			$hash .= $seed[mt_rand(0, $max)];
		}
		return $hash;
	}

	/**
	 * 字符串输出前的转义
	 * @param $string
	 * @param $flags
	 * @return array|mixed|string|string[]
	 */
	public static function htmlspecialchars($string, $flags = null) {
		if (is_array($string)) {
			foreach ($string as $key => $val) {
				$string[$key] = self::htmlspecialchars($val, $flags);
			}
		} else {
			if ($flags === null) {
				$string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
			} else {
				$charset = 'UTF-8';
				$string = htmlspecialchars($string, $flags, $charset);
			}
		}
		return $string;
	}

	/**
	 * 执行内存缓存命令
	 * @param $cmd string Cmd
	 * @param $key
	 * @param $value
	 * @param $ttl
	 * @return bool|mixed|null
	 */
	public static function memory($cmd, $key = '', $value = '', $ttl = 0) {
		list(, $c) = explode('/', $cmd);
		if (!$c) {
			$c = $cmd;
		}
		$class = 'Memory_Redis';
		$m = new $class();
		if (!$m->env()) {
			return null;
		}
		$argv = func_get_args();
		array_shift($argv);
		$return = null;
		switch ($c) {
			case 'set':
				$return = $m->set($key, $value, $ttl);
				break;
			case 'get':
				$return = $m->get($key);
				break;
			case 'rm':
				$return = $m->rm($key);
				break;
			case 'inc':
				$return = $m->inc($key, $value ?: 1);
				if ($ttl > 0) {
					$m->expire($key, $ttl);
				}
				break;
			case 'dec':
				$return = $m->dec($key, $value ?: -1);
				if ($ttl > 0) {
					$m->expire($key, $ttl);
				}
				break;
			default:
				if (!method_exists($m, $c)) {
					return null;
				}
				$return = call_user_func_array(array($m, $c), $argv);
		}
		return $return;
	}

	/**
	 * 批量查找字串位置
	 * @param $string
	 * @param $arr
	 * @param $returnvalue
	 * @return bool|mixed
	 */
	public static function strpos($string, $arr, $returnvalue = false) {
		if (empty($string)) return false;
		foreach ((array)$arr as $v) {
			if (strpos($string, $v) !== false) {
				$return = $returnvalue ? $v : true;
				return $return;
			}
		}
		return false;
	}

	/**
	 * 检测是否为手机环境
	 * @return bool
	 */
	public static function isMobile() {
		$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

		static $pad_list = array('ipad');
		if (self::strpos($useragent, $pad_list)) {
			return false;
		}

		static $touchbrowser_list = array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
			'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
			'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
			'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
			'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
			'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
			'benq', 'haier', '^lct', '320x320', '240x320', '176x220', 'windows phone');
		if (($v = self::strpos($useragent, $touchbrowser_list, true))) {
			return true;
		}

		static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
			'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
			'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
		if (($v = self::strpos($useragent, $wmlbrowser_list))) {
			return true;
		}

		$brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
		if (self::strpos($useragent, $brower)) return false;

		return false;
	}

}